<?php
/**
 * 数据字典
 */
namespace Admin\Controller;

use Common\Controller\AdminbaseController;

class DictController extends AdminbaseController
{

    protected $dict_model;

    public function _initialize()
    {
        parent::_initialize();
        $this->dict_model = D("Common/Dict");
    }

    /**
     * 数据字典列表
     */
    public function index()
    {
        $count = $this->dict_model->count();
        $page = $this->page($count, 12);
        $dicts = $this->dict_model->order('dict_code asc')->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign("page", $page->show('Admin'));
        $this->assign("dicts", $dicts);
        $this->display();
    }

    //添加数据字典
    public function add()
    {
        $this->display();
    }

    // 数据字典添加提交
    public function add_post()
    {
        if (IS_POST) {
            if (empty($_POST['dict_code']) || empty($_POST['dict_text'])) {
                $this->error("标识与描述不能为空！");
            }
            $dict['dict_code'] = trim(htmlspecialchars_decode($_POST['dict_code']));
            $dict['dict_text'] = trim(htmlspecialchars_decode($_POST['dict_text']));
            $data = $this->dict_model
                ->where(array("dict_code" => $dict['dict_code']))
                ->max('dict_value');
            if (empty($data)) {
                $dict['dict_value'] = 1;
            } else {
                $dict['dict_value'] = 1 + $data;
            }
            $result = $this->dict_model->add($dict);
            if ($result) {
                $this->success("添加成功！");
            } else {
                $this->error("添加失败！");
            }

        }
    }

    // 数据字典编辑
    public function edit()
    {
        $id = I("get.id", 0, 'intval');
        $data = $this->dict_model->where("id=$id")->find();
        $this->assign("data", $data);
        $this->display();
    }

    // 数据字典编辑提交
    public function edit_post()
    {
        if (IS_POST) {
            $id = intval($_POST['id']);
            if (empty($_POST['dict_code']) || empty($_POST['dict_text'])) {
                $this->error("标识与描述不能为空！");
            }
            $dict['id'] = $id;
            $dict['dict_code'] = trim(htmlspecialchars_decode($_POST['dict_code']));
            $dict['dict_text'] = trim(htmlspecialchars_decode($_POST['dict_text']));
            $result = $this->dict_model->save($dict);
            if ($result !== false) {
                $this->success("保存成功！");
            } else {
                $this->error("保存失败！");
            }
        } else {
            $this->error("该数据已经存在！");
        }
    }

    /**
     * 根据标识获得数据字典数据
     */
    public function get_dict()
    {
        $code = $_REQUEST['dict_code'];
        if (empty(code)) {
            return '';
        } else {
            $dict_list = $this->dict_model->where("dict_code=$code")->select();
            return $dict_list;
        }
    }

    // 数据字典删除
    public function delete()
    {
        $id = I("get.id", 0, 'intval');
        if ($this->dict_model->delete($id) !== false) {
            $this->success("删除成功！");
        } else {
            $this->error("删除失败！");
        }
    }
}

